﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ProjectWebNhac.BUS;
using ProjectWebNhac.DTO;
using System.Web.Services;

namespace ProjectWebNhac.GUI.Admin
{
    public partial class QL_Album : System.Web.UI.Page
    {
        AlbumBus abbus = new AlbumBus();
        BaiHatBus bhbus = new BaiHatBus();
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadgvDSAlbum();
                LoadBaiHat();
            }
        }
        private void CreateFriendlyGrid(GridView grView)
        {
            grView.UseAccessibleHeader = true;
            grView.HeaderRow.TableSection = TableRowSection.TableHeader;
            grView.FooterRow.TableSection = TableRowSection.TableFooter;
        }
        private void LoadgvDSAlbum()
        {
            DataTable dt = abbus.LayDanhSachAlbum();
            gvDSAlbum.DataSource = dt;
            gvDSAlbum.DataBind();
            CreateFriendlyGrid(gvDSAlbum);
        }

        protected void gvDSAlbum_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            if (e.CommandName.Equals("ChiTiet"))
            {

                int MaAlbum = Convert.ToInt16(e.CommandArgument);
                dt = abbus.LayChiTietAlbum(MaAlbum);
                txtTenAlbum.Text = dt.Rows[0]["TenAlbum"].ToString();
                txtTenCaSi.Text = dt.Rows[0]["TenCaSi"].ToString();
                txtLuotNghe.Text = dt.Rows[0]["LuotNghe"].ToString();
                txtNgayPhatHanh.Text = dt.Rows[0]["NgayPhatHanh"].ToString();
                txtTheLoaiNhac.Text = dt.Rows[0]["TenTheLoai"].ToString();
                imgHinhAnh.ImageUrl = "~/HinhAnh/Album/" + dt.Rows[0]["HinhAnh"].ToString();
                LoadgvDSAlbum();
                divEdit.Visible = true;
                AddSong.Visible = true;
                AlbumDTO objAlbum = new AlbumDTO();
                objAlbum.MaAlbum = MaAlbum;
                objAlbum.TenAlbum = txtTenAlbum.Text;
                objAlbum.TenCasi = txtTenCaSi.Text;
                objAlbum.HinhAnh = dt.Rows[0]["HinhAnh"].ToString();
                objAlbum.NgayPhatHanh = DateTime.Parse(txtNgayPhatHanh.Text);
                objAlbum.TenTheLoai = txtTheLoaiNhac.Text;
                Session["CurrentAlbum"] = objAlbum;
                btnUpdate.Visible = false;
                gvDSBaiHatAlbum.DataSource = bhbus.LoadDanhSachBaiHatTheoAlbum(MaAlbum);
                gvDSBaiHatAlbum.DataBind();
                if (gvDSBaiHatAlbum.Rows.Count>0)
                {
                    CreateFriendlyGrid(gvDSBaiHatAlbum);
                }
                LoadBaiHat();
                txtMaAlbum.Text = MaAlbum.ToString();
            }
            if (e.CommandName.Equals("Xoa"))
            {
                if (abbus.XoaAlbum(Convert.ToInt16(e.CommandArgument)))
                {
                    Response.Redirect("QL-Album.aspx");
                }
                
            }
            else if (e.CommandName.Equals("KichHoat"))
            {
                if (abbus.KichHoatAlbum(Convert.ToInt16(e.CommandArgument)))
                {
                    LoadgvDSAlbum();
                }
            }
        }

        protected void lnkThem_Click(object sender, EventArgs e)
        {
            if (lnkThem.Text.Equals("Thêm"))
            {
                EnableAdd();
            }
            else if (lnkThem.Text.Equals("Lưu"))
            {
                AlbumDTO objAlbum = new AlbumDTO();
                if (flhinhanh.FileName.Length > 0)
                {
                    objAlbum.HinhAnh = flhinhanh.FileName;
                    flhinhanh.SaveAs(Server.MapPath("~/HinhAnh/Album/" 
                        + flhinhanh.PostedFile.FileName));
                }
                else
                {
                    objAlbum.HinhAnh = "male-avatar-icon-614x460.png";
                }
                objAlbum.TenCasi = txtTenCaSi.Text;
                objAlbum.TenTheLoai = txtTheLoaiNhac.Text;
                objAlbum.TenAlbum = txtTenAlbum.Text;
                objAlbum.NgayPhatHanh = DateTime.Parse(txtNgayPhatHanh.Text);
                if (abbus.ThemAlbum(objAlbum))
                {
                    DisableAdd();
                    LoadgvDSAlbum();
                }
            }
        }
        private void EnableAdd()
        {
            txtTenAlbum.Enabled = true;
            txtTenAlbum.Text = "";
            txtTenCaSi.Enabled = true;
            txtTenCaSi.Text = "";
            txtNgayPhatHanh.Enabled = true;
            txtNgayPhatHanh.Text = "";
            flhinhanh.Enabled = true;
            txtTheLoaiNhac.Enabled = true;
            txtTheLoaiNhac.Text = "";
            lnkHuy.Visible = true;
            btnUpdate.Visible = false;
            lnkThem.Text = "Lưu";
        }
        private void DisableAdd()
        {
            txtTenAlbum.Enabled = false;
            txtTenAlbum.Text = "";
            txtTenCaSi.Enabled = false;
            txtTenCaSi.Text = "";
            txtNgayPhatHanh.Enabled = false;
            txtNgayPhatHanh.Text = "";
            flhinhanh.Enabled = false;
            txtTheLoaiNhac.Enabled = false;
            txtTheLoaiNhac.Text = "";
            lnkHuy.Visible = false;
            lnkThem.Text = "Thêm";
            btnUpdate.Visible = false;
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            AlbumDTO objAlbum = (AlbumDTO)Session["CurrentAlbum"];
            objAlbum.TenAlbum = txtTenAlbumEdit.Text;
            objAlbum.NgayPhatHanh = DateTime.Parse(txtNgayPHEdit.Text);
            objAlbum.TenCasi = txtTenCSEdit.Text;
            objAlbum.TenTheLoai = txtTheLoaiEdit.Text;
            if (flHinhAnhEdit.FileName.Length>0)
            {
                objAlbum.HinhAnh = flHinhAnhEdit.FileName;
                try
                {
                    string path=Server.MapPath("~/HinhAnh/Album/" + flHinhAnhEdit.PostedFile.FileName);
                    flHinhAnhEdit.PostedFile.SaveAs(path);
                }
                catch (System.Exception ex)
                {
                	
                }
               
            }
            if (abbus.SuaAlbum(objAlbum))
            {
                Session.Remove("CurrentAlbum");
                DisableAdd();
                LoadgvDSAlbum();
            }
        }

        protected void lnkHuy_Click(object sender, EventArgs e)
        {
            DisableAdd();
            LoadgvDSAlbum();
        }

        protected void gvDSBaiHatAlbum_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Khóa"))
            {
                
            }
            else if (e.CommandName.Equals("Mở"))
            {

            }
        }
        protected void gvDSAlbum_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lblTrangThai = (Label)e.Row.FindControl("lblTrangThai");
                LinkButton lnkXuLy = (LinkButton)e.Row.FindControl("lnkXoa");
                LinkButton lnkKichHoat = (LinkButton)e.Row.FindControl("lnkKichHoat");
                if (lblTrangThai.Text.Equals("True"))
                {
                    lblTrangThai.CssClass = "btn btn-success";
                    lblTrangThai.Text = "Đang Mở";
                    lnkXuLy.Visible = true;
                    lnkKichHoat.Visible = false;
                }
                else
                {
                    lblTrangThai.CssClass = "btn btn-primary input-top-margin";
                    lblTrangThai.Text = "Đang Khóa";
                    lnkXuLy.Visible = false;
                    lnkKichHoat.Visible = true;
                }
            }
        }

        protected void gvDSBaiHatAlbum_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                Label lbTrangThai = (Label)e.Row.FindControl("lbBhTrangThai");
                LinkButton lnkXuLy = (LinkButton)e.Row.FindControl("lnkXoa");
                if (lbTrangThai.Text == "True")
                {
                    lbTrangThai.CssClass = "btn btn-success";
                    lbTrangThai.Text = "Đang Mở";
                    lnkXuLy.Text = "Xóa";
                    lnkXuLy.CommandName = "Xóa";
                }
                else
                {
                    lbTrangThai.CssClass = "btn btn-primary input-top-margin";
                    lbTrangThai.Text = "Khóa";
                    lnkXuLy.Visible = true;
                }
            }
        }

        private void LoadBaiHat()
        {
            BaiHatBus bhbus = new BaiHatBus();
            gvSong.DataSource = bhbus.LayDsBHThemAlbum();
            gvSong.DataBind();
        }
        [WebMethod(EnableSession = false)]
        public static bool AddAlbum(int MaAlbum, int MaBaiHat)
        {
            AlbumBus abBus = new AlbumBus();
            if (abBus.ThemBaiHatVaoAlbum(MaAlbum,MaBaiHat))
            {
                return true;
            }
            return false;

        }
    }
}